//
//  LCTopTitleImageBar.m
//  HighCopyTodayNews
//
//  Created by hack on 2017/7/31.
//  Copyright © 2017年 hack. All rights reserved.
//

#import "LCTopTitleImageBar.h"

@interface LCTopTitleImageBar ()

@end

@implementation LCTopTitleImageBar

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (instancetype)initWithType:(NSInteger)type {
    if(self = [super init]) {
        self.type = 1;
        [self setupSubviews];
    }
    return self;
}


- (void)setupSubviews {
    self.frame = CGRectMake(0, 0, ScreenWidth, 40);
    [self addSubview:self.leftIconBtn];
    [self addSubview:self.centerView];
    [self addSubview:self.rightUIButton];
    [self.centerView addSubview: self.serachImg];
    [self.centerView addSubview: self.searchTipLable];
    [self.leftIconBtn sizeToFit];
    
    [self.leftIconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(-ScreenWidth/2 + 10);
        make.width.mas_equalTo(87.67);
        make.height.mas_equalTo(25);
        make.centerY.equalTo(self.mas_centerY).offset(0);
    }];
    [self.rightUIButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_centerX).offset(ScreenWidth/2 - 6);
        //make.width.mas_equalTo(self.rightUIButton.mas_width);
        make.width.mas_equalTo(63);
        make.height.mas_equalTo(25);//32);
        make.centerY.equalTo(self.leftIconBtn.mas_centerY).offset(0);
    }];
    ///白色搜索框
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.type == 1) {
            make.left.equalTo(self.leftIconBtn.mas_right).offset(10);
            //make.right.equalTo(self.mas_centerX).offset(ScreenWidth/2 - 10);
            make.right.equalTo(self.rightUIButton.mas_left).offset(-5);
        }else{
            self.rightUIButton.hidden = YES;
            make.left.equalTo(self.leftIconBtn.mas_right).offset(10);
            make.right.equalTo(self.mas_centerX).offset(ScreenWidth/2 - 10);
        }
//         make.bottom.equalTo(self.leftIconBtn).offset(0);
        make.centerY.equalTo(self.leftIconBtn).offset(0);
         make.height.mas_equalTo(self.leftIconBtn.mas_height).offset(5);
    }];
    [self.serachImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.centerView).offset(15);
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.centerY.equalTo(self.centerView);
    }];

    [self.searchTipLable  mas_makeConstraints:^(MASConstraintMaker *make) {
          make.centerY.equalTo(self.centerView);
          make.left.equalTo(self.serachImg.mas_right).offset(10);
//        if (self.type == 1) {
//            make.right.equalTo(self.centerView.mas_right).offset(-10);
//        }
    }];
}
- (void)serachClick {
    if (_serachBlock != nil) {
        if (_isPushSerach) {
            _serachBlock();
        }
    }
}
- (void)rightClick {
    if (_rightBlock != nil) {
        _rightBlock();
    }
}
-(instancetype)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        
    }
    return  self;
}

- (UIImageView *)leftIconBtn {
    if(!_leftIconBtn){
        _leftIconBtn = [UIImageView new];
        _leftIconBtn.image=[UIImage imageNamed:@"jkfylogo"];
//        _leftIconBtn.layer.cornerRadius=16;
//        _leftIconBtn.layer.masksToBounds=YES;
    }
    return _leftIconBtn;
}

- (UIView*)centerView {
    if(!_centerView){
        _centerView = [UIView new];
        //_centerView.layer.cornerRadius = 12;//4
        _centerView.backgroundColor = [UIColor whiteColor];
        _centerView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(serachClick)];
        [_centerView addGestureRecognizer:tapGesturRecognizer];
    }
    return _centerView;
}

- (UIButton *)rightUIButton {
    if(!_rightUIButton){
        _rightUIButton = [UIButton new];
        _rightUIButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_rightUIButton setTitle:@"发布文章" forState:(UIControlStateNormal)];
        //[_rightUIButton setImage:[UIImage imageNamed:@"分段计时-时钟"] forState:(UIControlStateNormal)];
//        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(serachClick)];
//        [_rightView addGestureRecognizer:tapGesturRecognizer];
        [_rightUIButton addTarget:self action:@selector(rightClick) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _rightUIButton;
}
- (UIImageView*)serachImg {
    if(!_serachImg){
        _serachImg = [UIImageView new];
        _serachImg.image = [UIImage imageNamed:@"灰色搜索"];
    }
    return _serachImg;
}

- (UILabel *)searchTipLable {
    if(!_searchTipLable){
        _searchTipLable = [UILabel new];
        [LSZSpecialPackaging setLabel_14_5_Size:_searchTipLable];
        _searchTipLable.text = @"搜索圣经、赞美诗...";
        _searchTipLable.textColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.72 alpha:1.00];
    }
    return _searchTipLable;
}

//重写hitTest方法，去监听_centerView按钮的点击，目的是为了让超过的部分点击也有反应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    //将当前_centerView的触摸点转换坐标系，转换到发布按钮的身上，生成一个新的点
    CGPoint newP = [self convertPoint:point toView:_centerView];
    CGPoint newR = [self convertPoint:point toView:_rightUIButton];

    //判断如果这个新的点是在发布按钮身上，那么处理点击事件最合适的view就是发布按钮
    if ( [_centerView pointInside:newP withEvent:event]) {
        return _centerView;
    }else if ( [_rightUIButton pointInside:newR withEvent:event]) {
        return _rightUIButton;
    }else{//如果点不在_centerView身上，直接让系统处理就可以了
        return [super hitTest:point withEvent:event];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [_centerView setRadius:_centerView.height/2];
}
@end
